<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Com_search extends CI_Controller {
    private $rear = '';
    private $per_page = 2;
    private $total_rows = 6;
    function  __construct(){
        parent::__construct();
        if($this->session->userdata('ngonngu') && $this->session->userdata('ngonngu') == 'en'){
            $this->rear = '_en';
            $this->lang->load('atta','english');
        }else{
            $this->lang->load('atta','vietnam');
        }
    }
    function index(){
        $where = array();
        //print_r($_POST);
        $data['rear'] = $this->rear;
        if($_POST){
            $this->session->set_userdata('search',$_POST);
        }
        $offset = $this->uri->segment(2);
        $requestpost = $this->session->userdata('search');
        
       if($requestpost['manufacturer'] == 1){//tim iem theo mã
            $where['code']=$requestpost['code'];
       }elseif($requestpost['manufacturer'] == 2){//tim iem theo ten
            $this->db->like("title$this->rear",$requestpost['code']);
       }
        if($requestpost['category'] != 0){
           $where['category']=$requestpost['category']; 
        }$this->db->where($where);            
        $this->db->limit($this->per_page,$offset);
        $query = $this->db->get('product');
        $atta = $query->result();
        if(!empty($atta)){
            
            $this->db->where($where);
            if($requestpost['code'] != 'ID từ khóa...')
                $this->db->like('code',$requestpost['code']);
            $query2 = $this->db->get('product');
            $this->total_rows = $query2->num_rows();
            $this->phantrang(); 
            $data['content'] = $this->load->view('com_search/search',array('product'=> $atta,'rear'=>$this->rear),true);
        }
        else
            $data['content'] = 'Không tìm thấy sản phẩm nào';
        $setting = $this->common_model->getone('setting',array('id'=>1));
        $data['title'] = 'Tìm kiếm';
        $data['metades'] = $setting->metades;
        $data['metakey'] = $setting->metakey;
        $this->load->view('template',$data);
    }
    function module(){
        $data['category'] = $this->common_model->getdata('product_category');
        $data['manufacturer'] = $this->common_model->getdata('product_manufacturer');
        $this->load->view('com_search/module',$data);
    }
    function phantrang(){
        $this->load->library('pagination');
        $config['base_url'] = base_url().$this->uri->segment(1).'/';
        $config['total_rows'] = $this->total_rows;
        $config['per_page'] = $this->per_page;
        $config['uri_segment'] = 2;
        $config['num_links'] = 12;
        $this->pagination->initialize($config);
    }
}